package com.ygou.controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ygou.pojo.Users;
import com.ygou.service.UsersService;
import com.ygou.utils.MailUtil;

@Controller
public class ActivateMailController {
	
	@Autowired
	private UsersService usersService;
	//SpringMVC的映射
    @RequestMapping("/activatemail")
    public String hello(HttpServletRequest request,ModelMap model) throws IOException, AddressException, MessagingException, NoSuchAlgorithmException {
        //获取激活参数
        String email = request.getParameter("email");
        String token = request.getParameter("token");
        Long time = System.currentTimeMillis();
        Map<String,Object> map=new HashMap<String,Object>();
		map.put("userEmail",email);
        Users u = usersService.findUserInfoByWhere(map);
        if(u!=null) {
            if(u.getUserstaus()==0&&u.getActivatetime()!=1) {
                if(u.getActivatetime()<time) {
                    //过期--激活失败
                    u.setActivatetime(Long.parseLong("-1"));
                    //重新发送激活邮件
                    u = MailUtil.activateMail(u);
                    //重新设置了有效时间和token激活码
                    usersService.updateByPrimaryKey(u);
                    System.out.println("激活码超时重新发送");
                    model.put("result","-1");
                } else if (u.getActivatetime()>time){
                    //在时间内
                    u.setActivatetime(Long.parseLong("1"));
                    if(u.getToken().equals(token)) {
                        //在时间内且激活码通过，激活成功
                        u.setUserstaus((byte)1);
                        u.setCreatetime(new Date());
                        //重新设置token防止被禁用的用户利用激活
                        u.setToken(token.replace("1", "c"));
                        usersService.updateByPrimaryKey(u);
                        System.out.println("激活成功");
                        model.put("result","0");
                    } else {
                        //在时间内但是且激活码错误
                    	System.out.println("激活码错误");
                    	model.put("result","-2");
                    }
                }
                //u.getStatus()!=1判断结束
            } else if(u.getUserstaus()==1) {
                //已经被激活的重复点链接
                System.out.println("已经被激活的重复点链接");
                model.put("result","-3");
            }
        //u为空
        }else{
        	model.put("result","-4");
        }
        return "regValidate";
    }
}
